/**
 * CommandPanel.java
 * 
 * Copyright (C) 2012 - Thibault BUREL
 * 
 * This file is part of MathDraft.
 *
 * MathDraft is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MathDraft is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MathDraft.  If not, see <http://www.gnu.org/licenses/>
 *  
 */
package drk.mathdraft.ihm.command;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import drk.mathdraft.ihm.EqButton;
import drk.mathdraft.ihm.MainPanel;
import drk.mathdraft.rules.RuleException;

public class CommandPanel extends JTabbedPane implements ActionListener {
	
	private static final long serialVersionUID = 1L;

	protected EqButton rule1;
	protected EqButton rule2;
	protected EqButton rule3;
	
	public CommandPanel () {
		this.add("Real Calculus", populateRCalculusPane());
	}
	public void actionPerformed(ActionEvent e)
	{
		try
		{		
			if(e.getSource() == rule1)
			{
				MainPanel.getInstance().applyRule1();
			}
			if(e.getSource() == rule2)
			{
				MainPanel.getInstance().applyRule2();
			}
			if(e.getSource() == rule3)
			{
				MainPanel.getInstance().applyRule3();
			}
		}
		catch(RuleException exc)
		{
			javax.swing.JOptionPane.showMessageDialog(null,exc.getMessage());
		}
	}
	
	private JTabbedPane populateRCalculusPane()
	{
		JTabbedPane result = new JTabbedPane();
		
		result.add("Addition", populateRCalculusAdditionPane());
		result.add("Multiplication", populateRCalculusMultiplicationPane());
		result.add("Quotient", populateRCalculusDivisionPane());
		return result;
	}
	
	private JPanel populateRCalculusAdditionPane()
	{
		JPanel result = new JPanel();
		
		result.setLayout(new GridLayout(0,4));
		
		this.rule1 = new EqButton ("a=b\\Leftrightarrow a+c=b+c");
		
		result.add(this.rule1);
		
		this.rule1.addActionListener(this);
		
		for(int i = 0; i < 15 ; i++)
		{
			JButton b = new JButton();
			result.add(b);
			b.setVisible(false);
		}
		
		return result;
	}
	
	private JPanel populateRCalculusMultiplicationPane()
	{
		JPanel result = new JPanel();
		
		result.setLayout(new GridLayout(0,4));
		
		this.rule2 = new EqButton ("a=b\\Leftrightarrow ac=bc");
		result.add(this.rule2);
		
		this.rule2.addActionListener(this);
		
		for(int i = 0; i < 15 ; i++)
		{
			JButton b = new JButton();
			result.add(b);
			b.setVisible(false);
		}
		
		return result;
	}
	
	private JPanel populateRCalculusDivisionPane()
	{
		JPanel result = new JPanel();
		
		result.setLayout(new GridLayout(0,4));
		
		this.rule3 = new EqButton ("\\frac{0}{c}=0");
		result.add(this.rule3);
		result.add(new EqButton ("\\frac{a}{1}=a"));
		result.add(new EqButton ("\\frac{ad}{cd}=\\frac{a}{c}"));
		result.add(new EqButton ("-\\frac{a}{c}=\\frac{-a}{c}=\\frac{a}{-c}"));
		
		result.add(new EqButton ("\\frac{a}{d}+\\frac{b}{d}=\\frac{a+b}{d}"));
		result.add(new EqButton ("\\frac{a}{c}+\\frac{b}{d}=\\frac{ac+bd}{cd}"));
		result.add(new EqButton ("a\\times\\frac{b}{c}=\\frac{ab}{c}"));
		result.add(new EqButton ("\\frac{a}{c}\\times\\frac{b}{d}=\\frac{ab}{cd}"));
		
		result.add(new EqButton ("\\frac{1}{\\frac{c}{d}}=\\frac{d}{c}"));
		result.add(new EqButton ("\\frac{\\frac{a}{b}}{\\frac{c}{d}}=\\frac{ad}{bc}"));
		result.add(new EqButton ("\\frac{\\frac{a}{b}}{c}=\\frac{a}{bc}"));
		result.add(new EqButton ("\\frac{a}{\\frac{b}{c}}=\\frac{ac}{b}"));
		
		result.add(new EqButton ("\\frac{a}{c}=0\\Leftrightarrow a=0"));
		result.add(new EqButton ("\\frac{a}{c}=\\frac{b}{d}\\Leftrightarrow ad=bc"));
		
		for(int i = 0; i < 2 ; i++)
		{
			JButton b = new JButton();
			result.add(b);
			b.setVisible(false);
		}
		
		this.rule3.addActionListener(this);
		
		return result;
	}
}